<?php
    @header("Content-Type:text/html;charset=utf-8");

    @include_once("conn.php");

    if(!(isset($_POST["type"])&&isset($_POST["id"]))){
        $obj = array();
        $obj["status"] = false;
        $obj["msg"] = "请传入完整参数";
        $json = json_encode($obj);
        exit($json);
    }

    $id = $_POST["id"];      //购物车的id
    $type = $_POST["type"];   //购物车的id
    
    // 更新
    // type = 0 => 减法 -1
    // type = 1 => 加法 +1
    // type = 2 => 失去焦点  直接改变内容  => buyNum

    if($type == 0){
        $sql = "update `shoppingcar` set buyNum = buyNum - 1 where id = '$id'";
    }else if($type == 1){
        $sql = "update `shoppingcar` set buyNum = buyNum + 1 where id = '$id'";
    }else if($type == 2){

        if(!(isset($_POST["buyNum"]))){
            $obj = array();
            $obj["status"] = false;
            $obj["msg"] = "请传入完整参数";
            $json = json_encode($obj);
            exit($json);
        }
        $buyNum = $_POST["buyNum"]; 

        $sql = "update `shoppingcar` set buyNum = $buyNum where id = '$id'";
    }
 



    // 执行mysql语句
    $result = mysqli_query($conn,$sql);
    // print_r($result);

    $obj = array();
    if($result){

        $rows = mysqli_affected_rows($conn);

        // echo $rows;
        if($rows>0){ // 更新成功
            $obj["status"] = true;
            $obj["msg"] = "更新成功";
        }else{
            $obj["status"] = true;
            $obj["msg"] = "更新成功,数据未改变";
        }


    }else{
        $obj["status"] = false;
        $obj["msg"] = "sql语句有误";
        $obj["sql"] = $sql;
    }

    echo json_encode($obj);

?>